package com.gas.gm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.gas.gm.pdu.DataProcessorContainer;
import com.gas.gm.pdu.GasDataProcessorFactory;
import com.gas.gm.pdu.MessageDataProcessor;
import com.gas.gm.pdu.msg.MessageStorage;
import com.gas.gm.pdu.session.GasGmSession;
import com.gas.gm.pdu.session.GasGmSessionListener;

public class BasicGasApp {

	static BufferedReader keyboard = new BufferedReader(new InputStreamReader(
			System.in));

	boolean keepRunning = true;
	private GasGmSessionListener listener;
	private GasDataProcessorFactory factory;
	private DataProcessorContainer container;
	private MessageStorage storage;
	
	public static void main(String[] args) throws IOException
	{
		BasicGasApp app=new BasicGasApp();
		app.menu();
	}

	protected void menu() throws IOException {
		keepRunning = true;
        String option = "1";
        int optionInt;
        
        while (keepRunning) {
            System.out.println();
            System.out.println("- 1 start");
            System.out.println("- 2 stop");
            System.out.println("- 3 active clients");
            System.out.println("- 4 stored messages");
           
            System.out.println("- 0 exit");
            System.out.print("> ");
            optionInt = -1;
            option = keyboard.readLine();
            optionInt = Integer.parseInt(option);
            switch (optionInt) {
			case 0:
				exit();
				break;
			case 1:
				start();
				break;
			case 2:
				stop();
				break;
			case 3:
				listClient();
				break;
			case 4:
				listMessages();
				break;
			default:
				 System.out.println("Invalid option. Choose between 0 and 4.");
	             break;
			}
        }
        System.out.println("Exiting simulator.");
	}
	
	protected void start() throws IOException{
		if(listener==null){
		  System.out.print("Enter port number> ");
	      int port = Integer.parseInt(keyboard.readLine());
	      System.out.print("Starting listener... ");
	      listener=new GasGmSessionListener(port, true);
	      container=new DataProcessorContainer();
	     storage=new MessageStorage();
	      factory=new GasDataProcessorFactory(container, storage);
	      listener.setProcessorFactory(factory);
	      listener.start();
	      System.out.println("started.");
		}else{
			 System.out.println("Listener is already running.");
		}
	}
	
	protected void stop() throws IOException{
		if(listener!=null){
			 System.out.println("Stopping listener...");
			 synchronized (container) {
				int count=container.size();
				MessageDataProcessor processor;
				GasGmSession session;
				for(int i=0;i<count;i++)
				{
					processor=(MessageDataProcessor) container.get(i);
					session=processor.getSession();
					 System.out.print("Stopping session "+i+": "+
                             processor.getSystemId() + " ...");
					 session.stop();
					 System.out.println(" stopped.");
				}
			}
			 listener.stop();
			 listener=null;
			 System.out.println("Stopped.");
		}
	}
	
	protected void exit() throws IOException{
		stop();
		keepRunning=false;
	}
	
	protected void listClient(){
		if(listener!=null){
			synchronized (container) {
				int count=container.size();
				if(count>0){
					MessageDataProcessor proc;
					for(int i=0;i<count;i++)
					{
						proc=(MessageDataProcessor) container.get(i);
						System.out.print(proc.getSystemId());
						if (!proc.isActive()) {
                            System.out.println(" (inactive)");
                        } else {
                            System.out.println();
                        }
					}
				}else{
					  System.out.println("No client connected.");
				}
			}
		} else {
            System.out.println("You must start listener first.");
        }
	}
	
	protected void listMessages(){
		if(listener!=null){
			storage.print();
		}else {
            System.out.println("You must start listener first.");
        }
	}
}
